package com.kcyu.sfherp.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kcyu.sfherp.entity.UserRecords;
import com.kcyu.sfherp.entity.utilEntity.UserInfoDTO;
import com.kcyu.sfherp.mapper.ms.SysMapper;
import com.kcyu.sfherp.service.SysService;
import com.kcyu.sfherp.service.UserRecordsService;
import com.kcyu.sfherp.utils.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="系统接口")
@RequestMapping("/sys")
public class SysController {

    @Autowired
    private SysService sysService;

    @Autowired
    private SysMapper sysMapper;

    @Autowired
    private UserRecordsService userRecordsService;

    @RequestMapping(value = "/login",method = {RequestMethod.POST})
    public ResponseResult Login(HttpServletResponse response, @RequestBody UserInfoDTO userinfo){
        return sysService.Login(userinfo, response);
    }

    @GetMapping(value = "/logout")
    public ResponseResult Logout(){
        return sysService.Logout();
    }

    @RequestMapping(value = "/loginNameget",method = {RequestMethod.GET})
    public Map<Object,Object> LoginNameGet(){
        List<String> users = sysMapper.getUsers();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", users);
        return map;
    }

    @GetMapping(value = "/getlog")
    public ResponseResult getLog(){
        return sysService.getLog();
    }

    @GetMapping(value = "/clearLog")
    public ResponseResult clearLog(){
        return sysService.clearLog();
    }

    @PostMapping(value = "/addRecord")
    public ResponseResult addRecord(@RequestBody UserRecords userRecords) {
        return userRecordsService.addRecord(userRecords);
    }

    @GetMapping(value = "/getAllRecords")
    public ResponseResult getAllRecords() {
        return userRecordsService.getAllRecords();
    }

    @PostMapping(value = "/queryRecords")
    public ResponseResult queryRecords(@RequestBody Map map) {
        Integer pageSize = (Integer) map.get("pageSize");
        Integer pageNum = (Integer) map.get("pageNum");
        UserRecords userRecord = JSON.parseObject(JSON.toJSONString(map.get("userRecord")), UserRecords.class);
        return userRecordsService.queryRecords(userRecord, pageNum, pageSize);
    }

    @GetMapping(value = "/queryActiveUser")
    public ResponseResult queryActiveUser() {
        return userRecordsService.queryActiveUser();
    }

    @GetMapping(value = "/deleteRecords/{id}")
    public ResponseResult deleteRecords(@PathVariable Integer id) {
        return userRecordsService.deleteRecords(id);
    }

}
